//
//  ShoppingCartView.m
//  Project two framework
//
//  Created by rimi on 16/7/28.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ShoppingCartView.h"
#import "ShoppingCartViewCell.h"
#import "ShoprDataSource.h"
#import "SubmitView.h"
#import "ListView.h"
#import "ZZConfig.h"
#import "ShoprDataSource.h"
#import "LUtilityTools.h"

@interface ShoppingCartView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIImageView               *imageview;
/**添加业务种类按钮*/
@property (strong, nonatomic) UIButton                  *addBtn;
@property (strong, nonatomic) UILabel                   *numberLabel;
/**业务种类*/
@property (strong, nonatomic) UILabel                   *businessLinesLabel;
/**业务说明*/
@property (strong, nonatomic) UILabel                   *businessExplainLabel;
/**保存订单*/
@property (strong, nonatomic) UIButton                  *saveIndentBtn;
/**提交办理*/
@property (strong, nonatomic) UIButton                  *submitBtn;
@property (strong, nonatomic) UITableView               *tableView;
@property (strong, nonatomic) NSMutableArray            *tableViewArray;
/**遮罩*/
@property (nonatomic, strong) UIImageView *otherImage;
/**搜索框*/
@property (nonatomic, strong) ListView  *listView;


@end

@implementation ShoppingCartView

-(instancetype)initWithFrame:(CGRect)frame
{

    self = [super initWithFrame:frame];
    if (self) {
        [self imageview];
        [self addBtn];
        [self numberLabel];
        [self businessLinesLabel];
        [self businessExplainLabel];
        [self saveIndentBtn];
        [self submitBtn];
        [self tableView];
        [self addSubview:self.otherImage];
        [self addSubview:self.listView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadDataTableView) name:@"ghyzz" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(removeTabelView) name:@"ppzz" object:nil];
        
      
    }
    return self;
}

#pragma mark --数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ShoprDataSource shareShopCar].shopCarSource.count;

}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIndex = @"ShoppingCartViewCell";
    ShoppingCartViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndex];
    if (cell == nil) {
        cell = [[ShoppingCartViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndex];
    }
    NSDictionary *dic = [ShoprDataSource shareShopCar].shopCarSource[indexPath.row];
    cell.numberTextField.text = dic[@"num"];
    cell.businessLinesCellLabel.text = dic[@"type"];
    cell.businessExplainCellLabel.text = dic[@"info"];
    cell.numberTextField.text = dic[@"num"];
    cell.businessLinesCellLabel.text = dic[@"name"];
    cell.cleanBtn.tag = indexPath.row + 200;
    [cell.cleanBtn addTarget:self action:@selector(cleanBtn:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

#pragma mark   ---  自定义方法
- (NSString *)numberWithString:(NSNumber *)number{
    NSString *string = [NSString stringWithFormat:@"%@",number];
    return string;
}



#pragma mark - 按钮的实现
- (void)btnAction:(UIButton *)sender
{
    [ShoprDataSource shareShopCar].tagZ = sender.tag;
    if ([ShoprDataSource shareShopCar].shopCarSource.count > 0) {
        [SubmitView showOtherView];
    }else{
        [LUtilityTools showAlertWithTitle:@"提示" message:@"购物车不能为空" buttonAction:nil];
    }
    
}

- (void)cleanBtn:(UIButton *)sender{
    /** 通知购物车的 numberlabel 减 1 */
    [[NSNotificationCenter defaultCenter] postNotificationName:@"minusOne" object:nil];
    
    [[ShoprDataSource shareShopCar].shopCarSource removeObjectAtIndex:sender.tag -200];
    [_tableView reloadData];
}

- (void)addBtnTap:(UIButton *)sender{
        _otherImage.alpha = 0.3;
        self.listView.hidden = NO;
}

- (void)closeTheAddBtn{
    self.otherImage.alpha = 0;
    self.listView.hidden = YES;
}

#pragma mark   ---  通知
- (void)reloadDataTableView{
    [_tableView reloadData];
}

- (void)removeTabelView{
    [[ShoprDataSource shareShopCar].shopCarSource removeAllObjects];
    [_tableView reloadData];
}

#pragma mark - 懒加载

-(UIImageView *)imageview
{
    if (_imageview == nil) {
        _imageview = [[UIImageView alloc]initWithFrame:self.bounds];
        _imageview.image = [UIImage imageNamed:@"down_05.png"];
        _imageview.userInteractionEnabled = YES;
        [self addSubview:_imageview];
    }

    return _imageview;
}

-(UIButton *)addBtn
{
    if (_addBtn == nil) {
        _addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _addBtn.frame = CGRectMake(800, 70, 40, 40);
        [_addBtn setImage:[UIImage imageNamed:@"+_10.png"] forState:UIControlStateNormal];
        [_addBtn addTarget:self action:@selector(addBtnTap:) forControlEvents:(UIControlEventTouchUpInside)];
        
        [_imageview addSubview:_addBtn];
    }
    return _addBtn;
}

-(UILabel *)numberLabel{
    if (_numberLabel == nil) {
        _numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(75, 125, 60, 20)];
        _numberLabel.font = [UIFont boldSystemFontOfSize:16];
        _numberLabel.textAlignment = NSTextAlignmentLeft;
        _numberLabel.textColor = [UIColor whiteColor];
        _numberLabel.text = @"数量";
        _numberLabel.backgroundColor = [UIColor clearColor];
        [_imageview addSubview:_numberLabel];
    }
    return _numberLabel;
}
//业务说明
-(UILabel *)businessExplainLabel{
    if (_businessExplainLabel == nil) {
        _businessExplainLabel = [[UILabel alloc]initWithFrame:CGRectMake(500, 125, 100, 20)];
        _businessExplainLabel.font = [UIFont boldSystemFontOfSize:16];
        _businessExplainLabel.text = @"业务说明";
        _businessExplainLabel.textAlignment = NSTextAlignmentLeft;
        _businessExplainLabel.textColor = [UIColor whiteColor];
        _businessExplainLabel.backgroundColor = [UIColor clearColor];
        [_imageview addSubview:_businessExplainLabel];
    }
    return _businessExplainLabel;
}
//业务种类
-(UILabel *)businessLinesLabel{
    if (_businessLinesLabel == nil) {
        _businessLinesLabel = [[UILabel alloc]initWithFrame:CGRectMake(190, 125, 100, 20)];
        _businessLinesLabel.font = [UIFont boldSystemFontOfSize:16];
        _businessLinesLabel.textAlignment = NSTextAlignmentLeft;
        _businessLinesLabel.textColor = [UIColor whiteColor];
        _businessLinesLabel.text = @"业务种类";
        _businessLinesLabel.backgroundColor = [UIColor clearColor];
        [_imageview addSubview:_businessLinesLabel];
    }
    return _businessLinesLabel;
}
-(UIButton *)saveIndentBtn
{
    if (_saveIndentBtn == nil) {
        _saveIndentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveIndentBtn.frame = CGRectMake(260, 560, 160, 50);
        [_saveIndentBtn setImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
        _saveIndentBtn.tag = 240;
        [_saveIndentBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_imageview addSubview:_saveIndentBtn];
    }
    return _saveIndentBtn;
}
-(UIButton *)submitBtn
{
    if (_submitBtn == nil) {
        _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _submitBtn.frame = CGRectMake(450, 560, 160, 50);
        [_submitBtn setImage:[UIImage imageNamed:@"提交办理.png"] forState:UIControlStateNormal];
        _submitBtn.tag = 241;
        [_submitBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [_imageview addSubview:_submitBtn];
    }
    return _submitBtn;
}

-(UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(30, 150, 810, 340) style:UITableViewStylePlain];
        _tableView.rowHeight =50;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        [_imageview addSubview:_tableView];
    }
    return _tableView;
}

- (UIImageView *)otherImage
{
    if (_otherImage == nil) {
        _otherImage = [[UIImageView alloc]initWithFrame:self.bounds];
        _otherImage.userInteractionEnabled = YES;
        _otherImage.alpha = 0;
        _otherImage.image = [UIImage imageNamed:@"down_08.png"];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTheAddBtn)];
        [_otherImage addGestureRecognizer:tap];
    }
    return _otherImage;
}

- (ListView *)listView{
        if (_listView == nil) {
            _listView = ({
                ListView *listView = [[ListView alloc]initWithFrame:CGRectMake(MIN_X(_addBtn.frame)-280, MIN_Y(_addBtn.frame), 280, 500)];
                listView.hidden = YES;
                listView.layer.cornerRadius = 20;
                listView.clipsToBounds = YES;
                listView;
            });
        }
          return _listView;
}



@end
